﻿using System;
using System.Collections.Generic;
using System.Linq;
using BKO.DataAccesss;
using BKO.DataAccesss.Repositories;
using BKO.Domain.Model;
using BKO.Domain.Services;
using BKO.Domain.Repositories;

namespace BKO.Services
{
	public class CompService : ICompService
	{
		private readonly IRepository<IComp> _repository;

		public CompService(string connectionString)
		{
			_repository = new CompRepository(BKODataContextSingletone.GetInstance(connectionString));
		}

		public IComp Get(int id)
		{
			return _repository.GetAll()
				.SingleOrDefault<IComp>(m => m.ID == id);
		}

		public List<IComp> GetAll()
		{
			return _repository.GetAll().ToList();
		}

		public List<IComp> GetAllByPlace(string filial, string otdel, string cabinet)
		{
			return _repository.GetAll()
				.Where(c => c.FILIAL == filial && c.MESTO == otdel && c.kabn == cabinet)
				.ToList();
		}

		public IComp Add()
		{
			return _repository.CreateEmpty();
		}

		public void Update(IComp comp)
		{
			if (comp.ID == 0)
			{
				_repository.Add(comp);
			}
			else
			{
				_repository.Update();
			}
		}

		public void Delete(int id)
		{
			_repository.Delete(Get(id));
		}
	}
}
